package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ReportDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @version 1.0
 * @auther gwt
 * @date 2021/6/28 14:51
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService{

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ReportDao reportDao;

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        /**
         * 获得运营统计数据
         * Map数据格式：
         * todayNewMember -> number
         * totalMember -> number
         * thisWeekNewMember -> number
         * thisMonthNewMember -> number
         *
         * todayOrderNumber -> number
         * todayVisitsNumber -> number
         * thisWeekOrderNumber -> number
         * thisWeekVisitsNumber -> number
         * thisMonthOrderNumber -> number
         * thisMonthVisitsNumber -> number
         * hotSetmeals -> List<Setmeal>
         */


        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        return result;

    }

    /**
     * 统计分析  每日预约  每日到诊
     */
    @Override
    public Map<String, Object> getNum4orderAndDiagnose(Date d1, long daysBetween) throws Exception {
        // list  1放所有日期 2放所有预约人数 3放所有到诊人数
        Map<String, Object> map = new HashMap<>();
        List<String> list1 = new ArrayList(); //日期集合
        List<Integer> list2 = new ArrayList();
        List<Integer> list3 = new ArrayList();
        for (long i = 0; i < daysBetween; i++) {
            String date = DateUtils.parseDate2String(d1);
            list1.add(date);//预约日期集合
            int order = reportDao.getNum4order(date);//预约人数集合
            list2.add(order);
            int diagnose = reportDao.getNum4Diagnose(date);//会诊人数
            list3.add(diagnose);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(d1);//1987-01-01
            calendar.add(Calendar.DATE, 1);
            d1 = calendar.getTime();
        }
        map.put("date1", list1);
        map.put("orderNum", list2);
        map.put("diagnoseNum", list3);
        return map;
    }

    /**
     * 统计 体检收入
     * @return
     */
    @Override
    public List<Map<String, Object>> income(String firstDay,String lastDay) {
        /* 	查询所欲套餐  放入 list
	遍历套餐  根据套餐名字和给定的日期范围查询 对应套餐的数量  放入map*/

        List<Map<String, Object>> list = new ArrayList<>();
       List<String> setmeals = reportDao.getSetmeals();//获取所有套餐
        if(setmeals!=null && setmeals.size()>=0){//查到了数据
            for (String setmeal : setmeals) {
                //在时间范围内  遍历获取每个套餐 已经就诊的数量  根据就诊数量计算总收入
                int diagnoseNum = reportDao.getDiagnoseNum(firstDay,lastDay,setmeal);
                Map<String , Object> map = new HashMap();
                map.put("name",setmeal);
                map.put("value",diagnoseNum);
                list.add(map);
            }
        }
        return list;

    }



    /**
     * 查询年龄分布
     * @return
     */
    @Override
    public List<Integer> getMemberAge() {
        int baby =0;//婴儿
        int young =0;//少年
        int youngman =0;//青年
        int mid =0;//中年
        int old =0;//老年
        List<Integer> list = new ArrayList<>();
       List<Integer> memberAges = reportDao.getMemberAge();
        System.out.println(memberAges);
       if (memberAges!=null && memberAges.size()>=0){//查到的所有年龄不为空
           for (Integer memberAge : memberAges) {//遍历年龄判断年龄段
               if (memberAge!=null){
                   if(memberAge<=6 && memberAge>=0 ){
                       baby++;
                   }else if(memberAge<=12 ){
                       young++;
                   }else if(memberAge<=45 ){
                       youngman++;
                   }else if(memberAge<=69){
                       mid++;
                   }else {
                       old++;
                   }
               }

           }
       }
       list.add(baby);
       list.add(young);
       list.add(youngman);
       list.add(mid);
       list.add(old);

      return list;
    }



}
